'use strict';
let fs = require('fs');
let path = require('path');
let router = require('koa-router')();


function souSuoZhuCheQi(dir){
let files=fs.readdirSync(dir);
return files.filter((name)=>{
    return name.endsWith('.js')&& name!=='index.js';
});
};


function dengLuWenJian(files){
files.forEach(item=>{
    let tmpPath=path.join(__dirname,item);
    let route = require(tmpPath);
    for(let key in route){
        let type=route[key][0];
        let fn=route[key][1];
        if(type==='get'){
            router.get(key,fn);
        }else if(type==='post'){
            router.get(key,fn);
        }else if(type==='delete'){
            router.get(key,fn);
        }else if(type==='put'){
            router.get(key,fn);
        }
    }
})
};


module.exports=function(correntDir){
let dir = correntDir || __dirname;


let controllerFile=souSuoZhuCheQi(dir);
dengLuWenJian(controllerFile);

return router.routes();
}
